package fzzl.discriminate

import com.alibaba.fastjson.JSONObject
import fzzl.Drug
import org.apache.commons.lang.StringEscapeUtils
import org.apache.commons.lang.StringUtils
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.jsoup.nodes.Element
import org.jsoup.select.Elements
import org.springframework.dao.DataIntegrityViolationException

import java.util.regex.Matcher
import java.util.regex.Pattern

class DiscriminateRecipeInfoController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        redirect(action: "list", params: params)
    }

    def list(Integer max) {
        params.max = Math.min(max ?: 1, 1)
        def list=DiscriminateRecipeInfo.findAll()
        def list1=DiscriminateRecipeInfo.list(params)
        int ii=0;
        for(DiscriminateRecipeInfo discriminateRecipeInfo:list)
        {
            String fangjie=discriminateRecipeInfo.cfyaopinming
            if(StringUtils.isBlank(fangjie)||"".equals(fangjie))
            {
                continue;
            }

            if(ii++>10)
            {
                fangjie=fangjie.substring(1,fangjie.length()-1);
            }
            fangjie=fangjie.replaceAll("\\\\/","/")
            fangjie=fangjie.replaceAll("\\\\r","")
            fangjie=fangjie.replaceAll("\\\\n","")
            fangjie=fangjie.replaceAll("\\\\t","")
            fangjie=fangjie.replaceAll("=\\\\","=")
            fangjie=fangjie.replaceAll("\\\\\"","\"")
            Document document=Jsoup.parseBodyFragment(fangjie)
            Element body=document.body()
            Elements elements=body.getElementsByClass("yaokuai");
            for(int i =0;i<elements.size();i++)
            {
                Element item=elements.get(i);
                String num=item.getElementsByClass("lab").text()
                print("num:"+num)
                String yongfa=item.getElementsByClass("yongfa").text()
                print("yongfa:"+yongfa)
                String mingcheng=item.getElementsByClass("mingcheng").text()
                mingcheng=decodeUnicode(mingcheng);
                print("mingcheng:"+mingcheng)
                String zlStr=item.getElementsByClass("zhongliang").text();
                String [] zlArray=zlStr.split("\\\\");
                String zhongliang=zlArray[0]
                if(null==zhongliang||""==zhongliang)
                {
                    zhongliang="0";
                }
                print("zhongliang:"+zhongliang)

                String danwei=zlArray[1]+"";
                danwei=decodeUnicode("\\"+danwei);
                print("danwei:"+danwei)
                DiscriminateRecipeDetailInfo  discriminateRecipeDetailInfo=new DiscriminateRecipeDetailInfo()
                Drug drug=Drug.findByDrugName(mingcheng)
                if(null==drug)
                {
                    drug=new Drug()
                    drug.drugName=mingcheng;
                    if(!drug.save()){
                        drug.errors.allErrors.each{
                            println(it)
                        }
                    }
                }
                discriminateRecipeDetailInfo.drug=Drug.findByDrugName(mingcheng)
                discriminateRecipeDetailInfo.discriminateRecipeInfo=discriminateRecipeInfo
                discriminateRecipeDetailInfo.rowNumber=Double.parseDouble(num)
                discriminateRecipeDetailInfo.makeType=yongfa
                discriminateRecipeDetailInfo.drugNum=Double.parseDouble(zhongliang)
                if(!discriminateRecipeDetailInfo.save()){
                    discriminateRecipeDetailInfo.errors.allErrors.each{
                        println(it)
                    }
                }
            }

            //fangjie=fangjie.substring(1,fangjie.length()-1)

            // fangjie=fangjie.replaceAll( "\\\\",  "");
//            JSONObject jsonObject=JSONObject.parseObject(fangjie)
//            println(jsonObject.toString())
        }
        [discriminateRecipeInfoInstanceList:list1 , discriminateRecipeInfoInstanceTotal: DiscriminateRecipeInfo.count()]
    }

    public String decodeUnicode( String dataStr) {
        int start = 0;
        int end = 0;
        final StringBuffer buffer = new StringBuffer();
        while (start > -1) {
            end = dataStr.indexOf("\\u", start + 2);
            String charStr = "";
            if (end == -1) {
                charStr = dataStr.substring(start + 2, dataStr.length());
            } else {
                charStr = dataStr.substring(start + 2, end);
            }
            char letter = (char) Integer.parseInt(charStr, 16); // 16进制parse整形字符串。
            buffer.append(new Character(letter).toString());
            start = end;
        }
        return buffer.toString();
    }




    def create() {
        [discriminateRecipeInfoInstance: new DiscriminateRecipeInfo(params)]
    }

    def save() {
        def discriminateRecipeInfoInstance = new DiscriminateRecipeInfo(params)
        if (!discriminateRecipeInfoInstance.save(flush: true)) {
            render(view: "create", model: [discriminateRecipeInfoInstance: discriminateRecipeInfoInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'discriminateRecipeInfo.label', default: 'DiscriminateRecipeInfo'), discriminateRecipeInfoInstance.id])
        redirect(action: "show", id: discriminateRecipeInfoInstance.id)
    }

    def show(Long id) {
        def discriminateRecipeInfoInstance = DiscriminateRecipeInfo.get(id)
        if (!discriminateRecipeInfoInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'discriminateRecipeInfo.label', default: 'DiscriminateRecipeInfo'), id])
            redirect(action: "list")
            return
        }

        [discriminateRecipeInfoInstance: discriminateRecipeInfoInstance]
    }

    def edit(Long id) {
        def discriminateRecipeInfoInstance = DiscriminateRecipeInfo.get(id)
        if (!discriminateRecipeInfoInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'discriminateRecipeInfo.label', default: 'DiscriminateRecipeInfo'), id])
            redirect(action: "list")
            return
        }

        [discriminateRecipeInfoInstance: discriminateRecipeInfoInstance]
    }

    def update(Long id, Long version) {
        def discriminateRecipeInfoInstance = DiscriminateRecipeInfo.get(id)
        if (!discriminateRecipeInfoInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'discriminateRecipeInfo.label', default: 'DiscriminateRecipeInfo'), id])
            redirect(action: "list")
            return
        }

        if (version != null) {
            if (discriminateRecipeInfoInstance.version > version) {
                discriminateRecipeInfoInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'discriminateRecipeInfo.label', default: 'DiscriminateRecipeInfo')] as Object[],
                          "Another user has updated this DiscriminateRecipeInfo while you were editing")
                render(view: "edit", model: [discriminateRecipeInfoInstance: discriminateRecipeInfoInstance])
                return
            }
        }

        discriminateRecipeInfoInstance.properties = params

        if (!discriminateRecipeInfoInstance.save(flush: true)) {
            render(view: "edit", model: [discriminateRecipeInfoInstance: discriminateRecipeInfoInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'discriminateRecipeInfo.label', default: 'DiscriminateRecipeInfo'), discriminateRecipeInfoInstance.id])
        redirect(action: "show", id: discriminateRecipeInfoInstance.id)
    }

    def delete(Long id) {
        def discriminateRecipeInfoInstance = DiscriminateRecipeInfo.get(id)
        if (!discriminateRecipeInfoInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'discriminateRecipeInfo.label', default: 'DiscriminateRecipeInfo'), id])
            redirect(action: "list")
            return
        }

        try {
            discriminateRecipeInfoInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'discriminateRecipeInfo.label', default: 'DiscriminateRecipeInfo'), id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'discriminateRecipeInfo.label', default: 'DiscriminateRecipeInfo'), id])
            redirect(action: "show", id: id)
        }
    }
}
